<!--柱状图-->
<template>
  <div :id="id" :ref="id" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'
require('echarts/theme/macarons') // echarts theme

const animationDuration = 1000
export default {
  name:"barChart",
  mixins: [resize],
  props: {
    id: {
      type: String,
      default: 'barChart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    chartData: {
      type: Object,
      default: () => ({
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        //右侧工具箱
        toolbox: {
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        grid: {
          top: 10,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: [{
          name: 'pageA',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [79, 52, 200, 334, 390, 330, 220],
          animationDuration
        }, {
          name: 'pageB',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [80, 52, 200, 334, 390, 330, 220],
          animationDuration
        }, {
          name: 'pageC',
          type: 'bar',
          stack: 'vistors',
          barWidth: '60%',
          data: [30, 52, 200, 334, 390, 330, 220],
          animationDuration
        }]
      })
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions()
      }
    }
  },
  mounted() {

  },
  beforeDestroy() {
    this.destroy()
  },
  methods: {
    /**
     * 初始化图表
     */
    initChart() {
      this.destroy()
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
      this.setClick()
    },
    /**
     * 更新图表数据
     */
    setOptions() {
      this.$nextTick(() => {
        this.chart.setOption(this.chartData)
      })
    },
    /**
     * 注册点击事件
     */
    setClick(){
      this.chart.getZr().off("click");
      let that = this
      this.chart.getZr().on("click", function (params) {
        let pointInPixel = [params.offsetX, params.offsetY];
        // 使用containPixel API判断点击位置是否在显示图形区域，下面的例子过滤了绘制图形的网格外的点击事件，好比X、Y轴lable、空白位置等的点击事件。
        if (that.chart.containPixel("grid", pointInPixel)) {
          let xIndex = that.chart.convertFromPixel({ seriesIndex: 0 }, [
            params.offsetX,
            params.offsetY,
          ])[0];
          let op = that.chart.getOption();
          //获取x轴坐标数据
          let xData=op.xAxis[0].data[xIndex]
          //获取所有y轴坐标数据
          let yData=[]
          op.series.forEach(item=>{
            yData.push({
              name:item.name,
              data:item.data[xIndex]
            })
          })
          that.$emit("onclick", xData,yData);
        }
      });
    },
    destroy() {
      if (this.chart) {
        this.chart.dispose()
        this.chart = null
      }
    }
  }
}
</script>
